package org.jdu.dao.metadata;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;

public class TableMetaData implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5104852643643382601L;
	
	private ColumnMetadata[] columnsMetadata;

	private Set set = new TreeSet(ColumnMetadata.COMPARATOR);
	
	private String tableCatalog;

	private String tableSchema;

	private String tableName;

	private String tableType;

	private String remarks;

	private String typeSchema;

	private String typeName;

	private String selfReferencingColName;

	private String refGeneration;

	private String typeCat;

	
	private void addColumn(ColumnMetadata columnsMetadata){
		set.add(columnsMetadata);
	}


	public ColumnMetadata[] getColumnsMetadata(ResultSet rs) throws SQLException {
		
		ResultSetMetaData rsmd = rs.getMetaData();
		int cols = rsmd.getColumnCount();
		Collection columns = new ArrayList();
		for (int c = 0; c < cols; c++) {
			columns.add(rsmd.getColumnName(c + 1));
		}
		while(rs.next()){
			ColumnMetadata columnMetadata = new ColumnMetadata();
			columnMetadata.setCharOctetLength(getColumnValue(columns,rs,DBMetaData.CHAR_OCTET_LENGTH));
			columnMetadata.setColumnDef(getColumnValue(columns,rs,DBMetaData.COLUMN_DEF));
			columnMetadata.setColumnName(getColumnValue(columns,rs,DBMetaData.COLUMN_NAME));
			columnMetadata.setColumnSize(getColumnValue(columns,rs,DBMetaData.COLUMN_SIZE));
			columnMetadata.setDataType(getColumnValue(columns,rs,DBMetaData.DATA_TYPE));
			columnMetadata.setDecimalDigits(getColumnValue(columns,rs,DBMetaData.DECIMAL_DIGITS));
			columnMetadata.setIsNullable(getColumnValue(columns,rs,DBMetaData.IS_NULLABLE));
			columnMetadata.setNullable(getColumnValue(columns,rs,DBMetaData.NULLABLE));
			columnMetadata.setNumPrecRadix(getColumnValue(columns,rs,DBMetaData.NUM_PREC_RADIX));
			columnMetadata.setOrdinalPosition(getColumnValue(columns,rs,DBMetaData.ORDINAL_POSITION));
			columnMetadata.setScopeCatalog(getColumnValue(columns,rs,DBMetaData.SCOPE_CATLOG));
			columnMetadata.setScopeSchema(getColumnValue(columns,rs,DBMetaData.SCOPE_SCHEMA));
			columnMetadata.setSqlDataType(getColumnValue(columns,rs,DBMetaData.SQL_DATA_TYPE));
			columnMetadata.setSqlDatetimeSub(getColumnValue(columns,rs,DBMetaData.SQL_DATETIME_SUB));
			columnMetadata.setTableCatalog(getColumnValue(columns,rs,DBMetaData.TABLE_CAT));
			columnMetadata.setTableName(getColumnValue(columns,rs,DBMetaData.TABLE_NAME));
			columnMetadata.setTableSchema(getColumnValue(columns,rs,DBMetaData.TABLE_SCHEM));
			columnMetadata.setTypeName(getColumnValue(columns,rs,DBMetaData.TYPE_NAME));
			set.add(columnMetadata);
		}
		
		columnsMetadata=new ColumnMetadata[set.size()];
		set.toArray(columnsMetadata);
		return columnsMetadata;
	}


	private String getColumnValue(Collection columns,ResultSet rs, String columnName) throws SQLException {
		if(columns.contains(columnName)){
			return rs.getString(columnName);
		}
		return null;
	}


	public ColumnMetadata[] getColumnsMetadata() {
		return columnsMetadata;
	}

	public Set getSet() {
		return set;
	}

	public String getTableCatalog() {
		return tableCatalog;
	}

	public String getTableSchema() {
		return tableSchema;
	}

	public String getTableName() {
		return tableName;
	}

	public String getTableType() {
		return tableType;
	}

	public String getRemarks() {
		return remarks;
	}

	public String getTypeSchema() {
		return typeSchema;
	}

	public String getTypeName() {
		return typeName;
	}

	public String getSelfReferencingColName() {
		return selfReferencingColName;
	}

	public String getRefGeneration() {
		return refGeneration;
	}

	public String getTypeCat() {
		return typeCat;
	}

	void setColumnsMetadata(ColumnMetadata[] columnsMetadata) {
		this.columnsMetadata = columnsMetadata;
	}

	void setSet(Set set) {
		this.set = set;
	}

	void setTableCatalog(String tableCatalog) {
		this.tableCatalog = tableCatalog;
	}

	void setTableSchema(String tableSchema) {
		this.tableSchema = tableSchema;
	}

	void setTableName(String tableName) {
		this.tableName = tableName;
	}

	void setTableType(String tableType) {
		this.tableType = tableType;
	}

	void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	void setTypeSchema(String typeSchema) {
		this.typeSchema = typeSchema;
	}

	void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	void setSelfReferencingColName(String selfReferencingColName) {
		this.selfReferencingColName = selfReferencingColName;
	}

	void setRefGeneration(String refGeneration) {
		this.refGeneration = refGeneration;
	}

	void setTypeCat(String typeCat) {
		this.typeCat = typeCat;
	}

}
